package shuati;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2023/12/9 22:33
 */
public class Demo02 {
/*    public static void main(String[] args) {
        int[] nums1 = {3,4,2,3};
        int[] nums2 = {1,5};
        int[] arr = findIntersectionValues(nums1,nums2);
        System.out.println(Arrays.toString(arr));
    }*/
    /*public static  int[] findIntersectionValues(int[] nums1, int[] nums2) {
        int[] arr = new int[2];
        int[] num1 = new int[101];
        int[] num2 = new int[101];
        Set<Integer> n1 = new HashSet<>();
        Set<Integer> n2 = new HashSet<>();

        for(int i = 0;i<nums1.length;i++){
            num1[nums1[i]]++;
            n1.add(nums1[i]);
        }
        for(int i = 0;i<nums2.length;i++){
            num2[nums2[i]]++;
            n2.add(nums2[i]);
        }

        int a = 0;
        int b = 0;
        for(int x:n1){
//            System.out.println(x);
            if (num2[x]>=1){
                a+=num1[x];
            }
        }
//        System.out.println(Arrays.toString(num1));
//        System.out.println(Arrays.toString(num2));
        for(int x:n2){
//            System.out.println(x);
            if (num1[x]>=1){
                b+=num2[x];
            }
        }
        arr[0] = a;
        arr[1] = b;
        return arr;
    }*/


   /* public static void main(String[] args) {
        System.out.println(removeAlmostEqualCharacters("zyxyxyz"));
    }
    public static int removeAlmostEqualCharacters(String word) {
        char[] arr = word.toCharArray();
        int count=0;
        for(int i = 1;i<arr.length;i++) {
            if (arr[i] - arr[i - 1] <= 1 && arr[i] - arr[i - 1] >= -1) {
                count++;
                i++;
            }
        }
        return count;
    }*/


/*    public static void main(String[] args) {
        int[] nums = {1,2,1,2,1,2,1,2};
        System.out.println(maxSubarrayLength(nums,1));
    }*/
   /* public static  int maxSubarrayLength(int[] nums, int k) {
        int left = 0;
        int right = 0;
        int max = 0;
        HashMap<Integer, Integer> hash = new HashMap<>();
        while (right < nums.length){
            if (hash.getOrDefault(nums[right],0)+1<=k){
                hash.put(nums[right],hash.getOrDefault(nums[right],0)+1);
            }else{
                hash.put(nums[right],hash.getOrDefault(nums[right],0)+1);
                while (hash.get(nums[right])>k) {
                    hash.put(nums[left],hash.get(nums[left])-1);
                    left++;
                }
            }
            max = Math.max(max,right-left+1);
            right++;
        }
        return max;

    }*/


/*    public int countTestedDevices(int[] batteryPercentages) {
        int count = 0;
        for(int i = 0;i<batteryPercentages.length;i++){
            if (batteryPercentages[i]>0){
                count++;
                for(int j = i+1;j<batteryPercentages.length;j++){
                    batteryPercentages[j] = Math.max(0,batteryPercentages[j]-1);
                }
            }
        }
        return count;
    }*/


   /* public List<Integer> getGoodIndices(int[][] variables, int target) {
        List<Integer> list = new ArrayList<>();
        for(int i = 0;i<variables.length;i++){
            int a = variables[i][0];
            int b = variables[i][1];
            int c = variables[i][2];
            int m = variables[i][3];

            BigDecimal ab = new BigDecimal(a).pow(b);
            BigDecimal ab0 = ab.divideAndRemainder(new BigDecimal(10))[1];
            BigDecimal abc = ab0.pow(c);

            if (abc.divideAndRemainder(new BigDecimal(m))[1].intValue() == target) {
                list.add(i);
            }

        }
        return list;


    }
*/


    public static void main(String[] args) {
        int[] nums = {1,4,2,1};
        System.out.println(countSubarrays(nums,2));
    }
    public static long countSubarrays(int[] nums, int k) {
        int left = 0;
        int right = 0;

        int max = nums[0];
        //找最大的数
        for(int i = 0;i<nums.length;i++){
            max = Math.max(nums[i], max);
        }

        long count = 0;
        long ret = 0;
        while (left<nums.length && right<nums.length){
            while (right<nums.length && count<k){
                if (nums[right] == max){
                    count++;
                }
                right++;
            }
            right--;

            while (left<nums.length && count==k){
                if (nums[left]==max){
                    count--;
                }
                ret += nums.length-right;
                left++;
            }
            right++;

        }
        return ret;

    }



}





















